<?php

use think\Db;


//    更新关键词 sid
function updatekey()
{
    $rows=Db::table('tb_webkey')
        ->where([
            'sid' => 0,
            'state'=>1 //正常词
        ])->select();

    foreach ($rows as $k => $v) {
        if($rows[$k]['typeid']==1){
//                $rows[$k]['sid'] =1010;
            Db::table('tb_webkey')->where(['typeid' => 1, 'id'=>$rows[$k]['id']])->update(['sid'=>1010]);
        } elseif ($rows[$k]['typeid']==2){
//                $rows[$k]['sid'] =1015;
            Db::table('tb_webkey')->where(['typeid' => 2, 'id'=>$rows[$k]['id']])->update(['sid'=>1015]);

        } elseif ($rows[$k]['typeid']==3){
//                $rows[$k]['sid'] =7010;
            Db::table('tb_webkey')->where(['typeid' => 3, 'id'=>$rows[$k]['id']])->update(['sid'=>7010]);

        }elseif ($rows[$k]['typeid']==4){
//                $rows[$k]['sid'] =1030;
            Db::table('tb_webkey')->where(['typeid' => 4, 'id'=>$rows[$k]['id']])->update(['sid'=>1030]);
        }elseif ($rows[$k]['typeid']==5){
//                $rows[$k]['sid'] =7070;
            Db::table('tb_webkey')->where(['typeid' => 5, 'id'=>$rows[$k]['id']])->update(['sid'=>7070]);
        }

    }
    return json($rows);

//        1:百度0
//        2:360
//        3:百度移动端
//        4:sogou
//        5:神马

//      1010://百度
//		1015://360
//		7010://百度移动
//		1030://搜狗
//		7070://神马
//		7015://360移动
//      7030://搜狗移动
//		7080: //头条

}

function is_login($name)
{
    if (empty($name)) {
        return false;
    } else {
        return true;
    }
}



function getPre($cid,$id)
{
    $list=Db::table('gg a')->field('a.*,c.title category_name')
        ->join('category c','c.id=a.cid')
        ->where([
            'a.cid'=>$cid,
            'a.id'=>['<',$id]
        ])
        ->order('a.id desc')
        ->find();
    return $list;
}

function getNext($cid,$id)
{
    $list=Db::table('gg a')->field('a.*,c.title category_name ')
        ->join('category c','c.id=a.cid')
        ->where([
            'a.cid'=>$cid,
            'a.id'=>['>',$id]
        ])
        ->order('a.id asc')
        ->find();
    return $list;
}



//排名监控 - 添加关键词
function jqcpm_add($sid,$key,$domain){
    $data = [];
    $uid = config('JQ_UID');
    $token = config('JQ_KEY');
    //sid 为客户系统搜索引擎标识id
    switch($sid){
        case 1010:
            $sid2 = 101;//百度
            break;
        case 1015:
            $sid2 = 102;//360
            break;
        case 1030:
            $sid2 = 103;//搜狗
            break;
        case 7010:
            $sid2 = 201;//百度移动
            break;
        case 7015:
            $sid2 = 202;//360移动
            break;
        case 7030:
            $sid2 = 203;//搜狗移动
            break;
        case 7070:
            $sid2 = 204;//神马
            break;
        case 7080:
            $sid2 = 205;//头条
            break;
    }

    $data = [
        'uid' => $uid,
        'token' => $token,
        'key' => $key,
        'domain' => $domain,
        'sid' => $sid2,
        'token' => $token,
        'jktime' => 6, //监控开始时间
        'cxtype' => 1,//监控查询
        'diqu' => 0,//地区目前只有一个
        'gid' => 230,//分组id,后台自己先添加
        'yd' => 0,//不核对异地排名
    ];


    $url = "http://api.xseo.vip:8910/api/cpm/add";
    $res = request_post($url,$data);
    return $res;

    /*
    结果json格式:
    成功  ['code'=>1,'tid'=>$tid];    请记录tid,方便删除操作
    失败  ['code'=>0,'msg'=>"添加失败"]
    */

}
//排名监控-删除
function jqcpm_del($tids){
    $data = [];
    $uid = config('JQ_UID');
    $token = config('JQ_KEY');

    $data = [
        'uid' => $uid,
        'token' => $token,
        'tid' => $tids,
    ];

    $url = "http://api.xseo.vip:8910/api/cpm/del";
    $res = request_post($url,$data);
    return $res;

    //结果json:['code'=>1,'msg'=>"success"]; 0失败1成功
}

function jqcpm_info($tids){
    $data = [];
    $uid = config('JQ_UID');
    $token = config('JQ_KEY');

    $data = [
        'uid' => $uid,
        'token' => $token,
        'tid' => $tids,
    ];

    $url = "http://api.xseo.vip:8910/api/cpm/info";
    $res = request_post($url,$data);
    return $res;
}

//排名监控 - 获取用户信息

function jqcpm_uinfo(){
    $data = [];
    $uid = config('JQ_UID');
    $token = config('JQ_KEY');

    $data = [
        'uid' => $uid,
        'token' => $token,
    ];

    $url = "http://api.xseo.vip:8910/api/cpm/uinfo";
    $res = request_post($url,$data);
    return $res;

    //返回结果json :['count'=>总关键词数,'c1'=>待查询数,'c2'=>待推送数];

}



/*
    * post 发送JSON 格式数据
    * @param $url string URL
    * @param $data_string string 请求的具体内容
    * @return array
    *      code 状态码
    *      result 返回结果
    */
function request_post($url='', $param='') {

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url); //抓取指定网页
    curl_setopt($curl, CURLOPT_HEADER, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串并输出到屏幕上
    curl_setopt($curl, CURLOPT_POST, 1); //post提交方式
    if($param != null && ! empty($param)){
        $o = "";
        foreach ($param as $k =>$v){
            $o .= "$k=" . urlencode($v) ."&";
        }
        $param=substr($o,0,-1);
        $post_data=$param;
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

    }
    $data=curl_exec($curl);
    curl_close($curl);
    return $data;

}